package com.yiwei.zxp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.adapter.PayTypeAdapter;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.GoodBuyInfoBean;
import com.yiwei.zxp.api.bean.GoodsPayBean;
import com.yiwei.zxp.api.bean.PayTypeBean;
import com.yiwei.zxp.api.model.GoodsBuyOrderModel;
import com.yiwei.zxp.api.model.GoodsPayModel;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.constant.Const;
import com.yiwei.zxp.constant.UserInfoGlobal;
import com.yiwei.zxp.event.PaySuccessEvent;
import com.yiwei.zxp.util.Arith;
import com.yiwei.zxp.util.Dialog;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.Util;
import com.yiwei.zxp.util.lianlian.env.EnvConstants;
import com.yiwei.zxp.util.lianlian.utils.BaseHelper;
import com.yiwei.zxp.util.lianlian.utils.Constants;
import com.yiwei.zxp.util.lianlian.utils.Md5Algorithm;
import com.yiwei.zxp.util.lianlian.utils.MobileSecurePayer;
import com.yiwei.zxp.util.lianlian.utils.PayOrder;
import com.yiwei.zxp.util.lianlian.utils.Rsa;
import com.yiwei.zxp.view.V19FrameLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.ls.LSInput;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class PayTypeActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_tips)
    TextView txtTips;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;

    private Unbinder unbiner;
    private PayTypeAdapter adapter;
    private ArrayList<String> orderData;
    private boolean is_preauth = false;
    private int pay_type_flag = 0;
    private String bank_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_type);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
        EventBus.getDefault().register(this);
    }

    private void init() {
        txtTitle.setText("云淘收银台");
        orderData = getIntent().getStringArrayListExtra("data");
        adapter = new PayTypeAdapter(this);
        adapter.setOnChooseListener(new PayTypeAdapter.OnChooseListener() {
            @Override
            public void onPayTypeSelect(String id, String name) {
                bank_name = name;
            }
        });
        list.setAdapter(adapter);
        getGoodBuyInfo();
        getPayWayList();
    }

    //    支付
    public void getPayWayList() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        PayTypeBean bean = new PayTypeBean();
        bean.type = 0;
        loadingDialog.show();
        Api.api_service.getPayWayList(bean).compose(RxLifeUtil.loginCheckOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
//                    UserInfoGlobal.setBankLimit(result.datas.bank_limit);
                    adapter.update(result.datas);
                });
    }

    public void getGoodBuyInfo() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        GoodBuyInfoBean bean = new GoodBuyInfoBean();

        bean.order_no = getGoodOrder();
        Api.api_service.getGoodBuyInfo(bean).compose(RxLifeUtil.loginCheckOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    btnSubmit.setText("支付" + Arith.add(result.datas.deliver_fee, result.datas.money) + "元");
                    txtTips.setText("您已用淘淘币抵扣了" + result.datas.shared_gold + "元，目前只需要支付" + Arith.add(result.datas.deliver_fee, result.datas.money) + "元。");
                });
    }

    public String getGoodOrder() {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < orderData.size(); i++) {

            builder.append(orderData.get(i));
            builder.append(",");

        }
        if (builder.length() != 0) {
            return builder.substring(0, builder.length() - 1);

        } else {
            return "";
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(PaySuccessEvent event) {
        finish();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
        EventBus.getDefault().unregister(this);

    }

    @OnClick({R.id.btn_back, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                showDilaog();
                break;
            case R.id.btn_submit:
                if (adapter.getSelectPoi() == -1) {
                    ToastUtil.makeText(this, "请选择支付方式");
                    return;
                }
                if (bank_name.equals("union")) {
                    Intent intent = new Intent(this, BankPayMoneyActivity.class);
                    intent.putStringArrayListExtra("data", orderData);
                    intent.putExtra("pay_type", Const.PayType.BUYER_ORDER);
                    startActivity(intent);
                } else {
                    ToastUtil.makeText(this, "暂不支持该方式，敬请期待");
                }
                break;
        }
    }

    @Override
    public void onBackPressed() {
        showDilaog();
    }

    private void showDilaog() {
        Dialog.showDefaultDialog(this, "", "订单24小时内会自动取消喔，您确定要离开吗？", "残忍离开", "继续支付", false, new Dialog.DialogDefaultClickListener() {
            @Override
            public void confirm() {

            }

            @Override
            public void cancel() {
                Util.startActivity(PayTypeActivity.this, BuyerOrderActivity.class);
                finish();
            }
        });
    }
}
